﻿/**
 * 异步加载列表数据
 */
function list(pageNo,whereValue)
{
	var url = getAction()+"?cmd=data&"+getForm().formSerialize();
	$.ajax({
		type: "POST",
		url: url,
		dateType: "html",
		success: function(returnedData){
			$("#dataList").get(0).innerHTML = returnedData;
			initParentIFrame();
		} 
	});
}

/**
 * 异步加载搜索列表数据
 */
function listSearch(pageNo,whereValue)
{
	var url = getAction()+"?cmd=search&"+getForm().formSerialize();
	$.ajax({
		type: "POST",
		url: url,
		dateType: "html",
		success: function(returnedData){
			$("#dataList").get(0).innerHTML = returnedData;
		}
	});
}
function getAction(){
	return "patentApply.action";
}

function getForm()
{
	return $("#patentApplyForm");
}

function getDomForm(){
	return $$("patentApplyForm");
}

/*
 * 跳转到添加页面
 */
function goAdd(){
	var height =550;
	if($$("orgId").value.trim()=='14'){
		height = 760;
	}
	var retValue = window.showModalDialog(getAction()+"?cmd=goAdd",window,"dialogWidth:550px;dialogHeight:"+height+"px;resizable:no;center:yes","menubar=no;toolbar=no;");
}

/*
 * 添加操作，然后返回list页面
 */
function add(){
	if(!validateForm()) return;
	var name = $$("po.patentName").value.trim();
	var type = $$("po.patentType").value.trim();
	var typeText = $("#po.patentType").find("option:selected").text();
	var condition = "patentname='"+name+"' and patenttype='"+type+"'";
	var maintainDep = $$("po.maintainDep").value;
	/*第一个参数表名
	 * 第二个参数是检查重复的条件,条件字段也是表里的字段
	 * 第三个参数是维护单位,在add.jsp页面添加这个值
	 */
	ValidateRepeat.checkRepeat("patent",condition,maintainDep,function(ret){
		var msg = $.evalJSON(ret);
		if(msg.processId=='1'){
			if(confirm(msg.deptName+'单位已添加了('+name+"+"+typeText+")专利名称+专利类型这条记录,您确定还要添加吗？")==false)
			return;
		}else if(msg.processId=='2'){
			alert("您所在单位已添加("+name+"+"+typeText+")专利名称+专利类型这条记录");
			return;
		}
		if(num==0){
			$$("hasAccessory").value = "NO";
		}
		$("#patentApplyForm").ajaxSubmit({
				dataType:"html",
				success : function(returnedData, statusText, xhr, $form) {
				returnedData = $.evalJSON(returnedData);
				if(returnedData.msg){
					alert(returnedData.msg);
				}
				if(returnedData.processId=='1'){
					if(num==0){
						var parentWindow = window.dialogArguments;
						parentWindow.list(1);
						window.close();
					}else{
						$('#uploadify').uploadify("settings","formData",{'formMap.sourceRecordId':returnedData.sourceRecordId});
						//sourceRecordId = returnedData.sourceRecordId;
						$('#uploadify').uploadify('upload','*')
					}
				}					
			}
		});
	});
}


/**
 * 添加后返回添加页面继续添加一个记录
 */
function addNext()
{
	if(!validateForm())	return;
	var url = getAction()+"?"+serializeForm(getDomForm());
	saveOrUpdate(url,"addNext");

}

/**
 * 跳转到更新页面
 */
function goUpdate(id)
{
	var height = 550;
	if($$("orgId").value.trim()=='14'){
		height = 770;
	}
	var retValue = window.showModalDialog(getAction()+"?cmd=goUpdate&po.id="+id,window,"dialogWidth:550px;dialogHeight:"+height+"px;resizable=no","menubar=no;toolbar=no");
	
}

/**
 * 更新一条记录
 */
function update()
{
	if(!validateForm()) return;
	var url = getAction()+"?"+serializeForm(getDomForm());
	$("#patentApplyForm").ajaxSubmit({
		dataType:"html",
		success : function(returnedData, statusText, xhr, $form) {
		returnedData = $.evalJSON(returnedData);
		if(returnedData.msg){
			alert(returnedData.msg);
		}
		if(returnedData.processId=='1'){
			var parentWindow = window.dialogArguments;
			parentWindow.list(1);
			window.close();
		}
	}
});
}

function saveOrUpdate(url,type){
	$.ajax({
		type: "POST",
		url: url,
		dateType: "html",
		success: function(returnedData){
			returnedData = $.evalJSON(returnedData);
			alert(returnedData.msg);
			if(returnedData.processId=='1'){
				var parentWindow = window.dialogArguments;
				parentWindow.list(1);
			}
			if(type !== 'addNext' )	//未定义
				window.close();
			getForm().reset();		//重置
		}
	});
}

/**
 * 表单域校验
 */ 
function validateForm()
{
	var success = true;
	if(!checkBlank($$("po.patentName"),"专利名称")||!MaxLength($$("po.patentName"),"专利名称",100))
	{
		success = false;
	}
	else if(!checkBlank($$("po.applicant"),"申请人(专利人)"))
	{
		success = false;
	}
	else if(!checkBlank($$("po.applyDate"),"申请时间")||!IsDate($$("po.applyDate"),"申请时间")) 	//||!checkNum($$("po.identityDate"),"鉴定日期"))
	{
		success = false;
	}	
	else if(!checkBlank($$("po.belongPlateFormID"),"所属平台"))
	{
		success = false;
	}
	
	return success;
}

function del(id){
	if(confirm("请确定是否删除该条记录？")==false)
		return;
	var url = getAction()+"?cmd=delete&formMap.ids="+id;
	$.ajax({
		type: "POST",
		url: url,
		dateType: "html",
		success: function(returnedData){
			returnedData = $.evalJSON(returnedData);
			alert(returnedData.msg);
			if(returnedData.processId=='1'){
				list(1);
			}
		}
	});
}

function lookView(id){
	var height = 490;
	if($$("orgId").value.trim()=='14'){
		height = 710;
	}
	var url = getAction()+"?cmd=goView&po.id="+id;
	window.showModalDialog(url,window,"dialogWidth:550px;dialogHeight:"+height+"px;resizable=no", "menubar=no;toolbar=no");
}

function upload(){
	$("#uploadForm").ajaxSubmit( {
			dataType:"html",
			success : function(returnedData, statusText, xhr, $form) {
			$("#dataList").get(0).innerHTML = returnedData;
		}
	});
}
var num=0;
$(document).ready(function() {
		if(!$$("path"))
		return;
		  var path = $$("path").value;
		  var cmdTemp = $$("cmd").value;
		  //var buttonImage = path+'/images/accessory.jpg';
		  var width = '70';  // update  1-29
		  var height = '18.5';
		  var auto = false;
		  var fileTypeExts = "*.*";
		  var cmd = cmdTemp;
		  var buttonText = '批量导入';
		  if(cmd=='batchAdd'){  // update  1-29
			  /*var width = '70';
			  var height = '20';
		  	  buttonImage = path+'/images/batchImport.jpg';*/
		  	  auto = true;
		  	  fileTypeExts = "*.xls";
		  }else{
		  	if(cmd=='update'){
		  		auto = true;
		  	}
		  	buttonText = '添加附件';// update  1-29
		  	cmd = "upload";
		  }
            $("#uploadify").uploadify({
		        'successTimeout':99999,
		        //附带值
		        'formData':{
		            'cmd':cmd,
		            'creator':'(@#$%^&*!~)',
		            'formMap.sourceRecordId':'0'
		        },
		        //flash
		        'swf': path+"/js/commom/uploadify.swf",
		        //不执行默认的onSelect事件
		        'overrideEvents' : ['onDialogClose'],
		        'auto':auto,
		        //文件选择后的容器ID
		        'queueID':'fileQueue',
		       // 'removeCompleted' : false,
		        //服务器端脚本使用的文件对象的名称 $_FILES个['upload']
		        'fileObjName':'accessory',
		        //上传处理程序
		       'uploader':path+"/patent/"+getAction(),
		        //浏览按钮的背景图片路径
		        //'buttonImage':buttonImage,
		        'buttonText':buttonText,  //update 1-29
		        //浏览按钮的宽度
		        'width':width,
		        //浏览按钮的高度
		        'height':height,
		        //expressInstall.swf文件的路径。
		        'expressInstall':path+"/js/commom/expressInstall.swf",
		        //在浏览窗口底部的文件类型下拉菜单中显示的文本
		        'fileTypeDesc':'支持的格式：',
		        //允许上传的文件后缀
		        'fileTypeExts':fileTypeExts,
		        //上传文件的大小限制
		        'fileSizeLimit':'120MB',
		        //上传数量
		        'queueSizeLimit' : 25,
		        //每次更新上载的文件的进展
		        'onUploadProgress' : function(file, bytesUploaded, bytesTotal, totalBytesUploaded, totalBytesTotal) {
		             //有时候上传进度什么想自己个性化控制，可以利用这个方法
		             //使用方法见官方说明
		        },
		        //选择上传文件后调用
		        'onSelect' : function(file) {
		        	num++;
		        },
		        'onCancel' : function(file) {
		            num--;
		        } ,
		        //返回一个错误，选择文件的时候触发
		        'onSelectError':function(file, errorCode, errorMsg){
		            switch(errorCode) {
		                case -100:
		                    alert("上传的文件数量已经超出系统限制的"+$('#uploadify').uploadify('settings','queueSizeLimit')+"个文件！");
		                    break;
		                case -110:
		                    alert("文件 ["+file.name+"] 大小超出系统限制的"+$('#uploadify').uploadify('settings','fileSizeLimit')+"大小！");
		                    break;
		                case -120:
		                    alert("文件 ["+file.name+"] 大小异常！");
		                    break;
		                case -130:
		                    alert("文件 ["+file.name+"] 类型不正确！");
		                    break;
		            }
		        },
		        //检测FLASH失败调用
		        'onFallback':function(){
		            alert("您未安装FLASH控件，无法导入批量文件！请安装FLASH控件后再试。");
		        },
		        'onUploadStart' : function(file) {
		        	if(cmdTemp=='update'){
		        		$('#uploadify').uploadify("settings","formData",{'formMap.sourceRecordId':$$("po.id").value});
		        	}
		        },
		        //上传到服务器，服务器返回相应信息到data里
		        'onUploadSuccess':function(file, data, response){
		        	if(cmd=='batchAdd'){
			            var msg = $.evalJSON(data);
		        		if(msg.processId=='1'){
		        			var t = msg.msg;
		        			alert(t); // update  1-29
		        			list();
		        		}else{
		        			//$$("msgData").innerHTML=msg.msg;
		        			//msgDialog.dialog("open");
		        			var width="550";
		        			var height="450";
		        			if(msg.height!='0'){
		        				width = 100 * msg.width;
		        				height = 200+60 * msg.height;
		        			}
		        			window.showModalDialog(getAction()+"?cmd=showBatchAddMsg&processId="+msg.processId,window,"dialogWidth:"+width+"px;dialogHeight:"+height+"px;resizable=no","menubar=no;toolbar=no");
		        		}
		        		
					}else{
						var returnedData = $.evalJSON(data);
						if(returnedData.processId != '1'){
							alert(file.name+"上传失败!");
						}
					}
		        },
		        'onInit'   : function(instance) {
            		num=0;
        		} ,
		        'onQueueComplete' : function(queueData) {
		        	if(cmd!='batchAdd'){
		        		if(queueData.uploadsErrored > 0){
			        		alert("上传文件失败！成功"+queueData.uploadsSuccessful+"个、失败"+queueData.uploadsErrored+"个");
		        		}else{
			        		alert("添加成功！");
							if(cmdTemp=='update'){
								updateAccessory();
							}else{
				        		var parentWindow = window.dialogArguments;
								parentWindow.list(1);
								window.close();
							}
		        		}
		        	}
		        } 
		    });
	});
	
function closeShowMsgDiv(){
	$("#showMsg").hide();
	$$("hide_select").style.display="none";
	list();
}

function updateAccessory(){
	var id = $$("po.id").value;	
	var url = getAction()+"?cmd=update&formMap.flag=accessory&formMap.sourceRecordId="+id;
	$.ajax({
		type: "POST",
		url: url,
		dateType: "html",
		success: function(returnedData){
			$$("accessoryData").innerHTML=returnedData;
			$("#showAccessory").show();
		}
	});
}

function delAccessory(id,attachmentName){
	var url = getAction()+"?cmd=delete&formMap.flag=accessory&formMap.ids="+id+"&formMap.attachmentName="+attachmentName;
	$.ajax({
		type: "POST",
		url: url,
		dateType: "html",
		success: function(returnedData){
			returnedData = $.evalJSON(returnedData);
			alert(returnedData.msg);
			if(returnedData.processId=='1'){
				updateAccessory();
			}
		}
	});
}

function exportData(){
	
	var url = getAction()+"?cmd=exportAll&formMap.order=applyDate";
	var property = $$("formMap.searchProperty")
	var key = $$("formMap.searchKey");
	if(property && key){
		url += "&formMap.searchProperty="+property.value +"&formMap.searchKey="+encodeURIComponent(key.value);
	}
	window.location = url;
}

function stateChange(obj){
	var rd = $$("po.resultDate");
	var rd2 = $$("po.resultDate2");
	if(obj.value=='2' || obj.value=='3'){
		rd.style.display='none';
		rd2.style.display='block';
	}else if(obj.value=='1'){
		rd.style.display='block';
		rd2.style.display='none';
	}
}